//
//  UILabel.swift
//  Networking
//
//  Created by 许必成 on 2020/7/22.
//  Copyright © 2020年 东信北邮信息技术有限公司. All rights reserved.
//
import UIKit

extension UILabel{
    
    /// 给UILabel设置间距
    ///
    /// - Parameters:
    ///   - lineS: 行间距
    ///   - widthS: 文字间距
    /// - Returns: 返回一个新的高度
    func lineSpacing(_ lineS:CGFloat = 0,_ widthS:CGFloat = 0) ->CGFloat {
        if self.text == nil { text = "label"}
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineBreakMode = .byWordWrapping
        paraStyle.alignment = .center
        paraStyle.lineSpacing = lineS
        paraStyle.hyphenationFactor = 1.0
        paraStyle.firstLineHeadIndent = 0.0
        paraStyle.paragraphSpacingBefore = 0.0
        paraStyle.headIndent = 0
        paraStyle.tailIndent = 0
        //设置字间距
        let dic = [NSAttributedString.Key.font:font,NSAttributedString.Key.paragraphStyle:paraStyle,NSAttributedString.Key.kern:widthS] as [NSAttributedString.Key : Any]
        
        let attributeStr = NSAttributedString.init(string: self.text!, attributes: dic)
        self.attributedText = attributeStr
        let rect = text?.boundingRect(with: CGSize(width: self.bounds.width,height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:dic, context: nil)
        return rect!.height
    }
}



